package com.vonchenchen.usbmuxd_android.usbmuxd.localconn.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

import static android.R.attr.port;

/**
 * Created by lidechen on 3/28/17.
 */

public class SocketReciver extends SocketConnector {

    private ServerSocket mSocketServer;
    private Socket mSocket;

    private InputStream mInputStream;
    private OutputStream mOutputStream;

    public SocketReciver(String address, int port) {
        super(address, port);
    }

    @Override
    public InputStream getInputStream() {
        return mInputStream;
    }

    @Override
    public OutputStream getOutputStream() {
        return mOutputStream;
    }

    @Override
    public void open() throws IOException {

        mSocketServer = new ServerSocket();
        //mCameraServer.setReceiveBufferSize(3 * 1024 * 1024);
        if(mReceiveBufferSize > 0) {
            mSocketServer.setReceiveBufferSize(mReceiveBufferSize);
        }
        mSocketServer.setReuseAddress(true);
        //mCameraServer.bind(new InetSocketAddress(55555));
        mSocketServer.bind(new InetSocketAddress(mPort));
    }

    public void accept() throws IOException{
        mSocket = mSocketServer.accept();
        mOutputStream = mSocket.getOutputStream();
        mInputStream = mSocket.getInputStream();
    }

    @Override
    public void close() throws IOException {

    }
}
